package com.trippin.DB.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Basic;

import com.google.appengine.api.datastore.Key;
import com.google.gson.annotations.Expose;
import com.trippin.Utils.StickyNoteComparator;

@PersistenceCapable
public class UserEntity implements TrippinEntity, Serializable {

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

	@Persistent
	@Expose
	private Long id; //not automatically generated - use FB user ID

	@Persistent
	@Expose
	private String name = "";
	
	@Persistent
	@Expose
	private String gender = "";
	
	@Persistent
	@Basic
	@Expose
	private Date birthday;
	
	@Persistent
	@Expose
	private String languages = "";
	
	@Persistent
	@Expose
	private String email = "";

	@Persistent
	@Expose
	private String info = "";
	
	@Persistent
	@Expose
	private List<SharingRequest> sharingTripsRequests;
	
	@Persistent
	@Expose
	Set<StickyNoteEntity> stickyNotes;
	
	@Persistent
	@Expose
	private Date createdOn;
	
	@Persistent
	@Expose
	private Date lastModifiedOn;

	
	public UserEntity(long userId) {
		this.id = userId;
	}

	public void initUerEntity(String name, String gender, 
		Date birthday, String spokenLanguages) {
		this.createdOn = new Date();
		this.lastModifiedOn = new Date();
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
		this.languages = spokenLanguages;
		this.sharingTripsRequests = new ArrayList<SharingRequest>();
		this.stickyNotes = new TreeSet<StickyNoteEntity>(new StickyNoteComparator());
	}
	
	public Key getKey() {
		return key;
	}

	public Date getLastModifiedOn() {
		return lastModifiedOn;
	}

	public Date getCreatedOn() {
		return createdOn;
	}

	public long getUserId() {
		return id;
	}
	public void setUserId(long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getGender() {
		return gender;
	}
	public void setBirthDay(Date birthDay) {
		this.birthday = birthDay;
	}
	public Date getBirthDay() {
		return birthday;
	}
	public void setLanguages(String languages) {
		this.languages = languages;
	}
	public String getLanguages() {
		return languages;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return email;
	}
	
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
	
	public List<SharingRequest> getSharingTripsRequests() {
		if(sharingTripsRequests == null) {
			sharingTripsRequests = new ArrayList<SharingRequest>(); 
		}
		return sharingTripsRequests;
	}
	
	public SharingRequest getSharingTripsRequestById(long requestId) {
		if(sharingTripsRequests != null) {
			for(SharingRequest req : sharingTripsRequests) {
				if(req.getId() == requestId) {
					return req;
				}
			}
		}
		return null;
	}
	
	public void removeSharingTripRequest(long requestId) {
		if(sharingTripsRequests == null) {
			sharingTripsRequests = new ArrayList<SharingRequest>();
			return;
		}
		SharingRequest reqForDelete = getSharingTripsRequestById(requestId);
		if(reqForDelete != null) {
			sharingTripsRequests.remove(reqForDelete);
		}
	}

	public void setSharingTripsRequests(List<SharingRequest> sharingTripsRequests) {
		this.sharingTripsRequests = sharingTripsRequests;
	}
	
	public void addSharingTripRequest(SharingRequest sharingTripsRequest) {
		if(this.sharingTripsRequests == null) {
			this.sharingTripsRequests = new ArrayList<SharingRequest>();
		}
		this.sharingTripsRequests.add(sharingTripsRequest);
	}

	public Set<StickyNoteEntity> getStickyNotes() {
		return stickyNotes;
	}

	public void addStickyNote(StickyNoteEntity stickyNote) {
		if(stickyNotes == null) {
			stickyNotes = new TreeSet<StickyNoteEntity>( new StickyNoteComparator());
		}
		stickyNotes.add(stickyNote);
	}
	
	public StickyNoteEntity getStickyNoteById(long stickyNoteId) {
		if(stickyNotes != null) {
			for(StickyNoteEntity sn : stickyNotes) {
				if(sn.getId() == stickyNoteId) {
					return sn;
				}
			}
		}
		return null;
	}

	public void removeStickyNote(long stickyNoteId) {
		if(stickyNotes != null) {
			StickyNoteEntity sn = getStickyNoteById(stickyNoteId);
			if(sn != null) {
				stickyNotes.remove(sn);
			}
		}
	}

	public void updateUser(UserEntity user) {
		if(user.getName() != null) {
			this.name = user.getName();
		}
		this.email = (user.getEmail() != null ? user.getEmail() : "");
		this.birthday = user.getBirthDay();
		this.gender = (user.getGender() != null ? user.getGender() : "");
		this.languages = (user.getLanguages() != null ? user.getLanguages() : "");
		this.info = (user.getInfo() != null ? user.getInfo() : "");
		this.lastModifiedOn = new Date();
	}

}
